package com.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.entity.SysMenu;
import com.blog.entity.SysUser;
import com.blog.service.SysMenuService;

import com.blog.util.dto.AjaxResult;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import com.blog.util.tree.XmTreeVo;
import com.blog.vo.menu.SaveMenuVo;
import com.blog.vo.menu.SysMainMenuVo;
import com.blog.vo.menu.SysMenuInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/menu")
public class SysMenuController {

    @Value("${system.config.welcomeUrl}")
    private String welcomeUlr;

    @Value("${system.config.adminLogoName}")
    private String adminLogoName;

    @Value("${system.config.logoUrl}")
    private String logoUrl;

    @Resource
    SysMenuService menuService;

    @GetMapping("/getSysMenu")
    @ResponseBody
    public Map<String, Object> getSysMenu(){
        Map<String, Object> map = new HashMap<>();

        SysMenuInfoVo homeInfo = new SysMenuInfoVo();
        homeInfo.setTitle("首页");
        homeInfo.setHref(welcomeUlr);
        map.put("homeInfo",homeInfo);

        SysMenuInfoVo logoInfo = new SysMenuInfoVo();
        logoInfo.setTitle(adminLogoName);
        logoInfo.setImage(logoUrl);
        logoInfo.setHref("");
        map.put("logoInfo",logoInfo);

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        List<SysMainMenuVo> menuInfo =menuService.getMenuInfo(sysUser.getId());

        map.put("menuInfo",menuInfo);
        return map;
    }

    @GetMapping("/admin/tableList")
    @ResponseBody
    public LayDto tableList(){
        List<Map<String,Object>> list = menuService.tableList();
        return LayDtoUtil.returnSuccess(0,"",list.size(),list);
    }

    @GetMapping("/admin/menu-add")
    public String menuAdd(){
        return "admin/system/menu/menu-add";
    }

    @GetMapping("/admin/queryMenuXmTree")
    @ResponseBody
    public AjaxResult queryMenuXmTree(@RequestParam(value = "selectIds",required = false) List<Long> menuIds){
        List<XmTreeVo> list = menuService.queryMenuXmTree(menuIds);
        return AjaxResult.success(list);
    }

    @PostMapping("/admin/saveMenu")
    @ResponseBody
    public AjaxResult saveMenu(@RequestBody SaveMenuVo menuVo){
        return menuService.saveMenu(menuVo);
    }

    @PostMapping("/admin/upOrDownSort")
    @ResponseBody
    public AjaxResult upOrDownSort(@RequestParam("sign") String sign,@RequestParam("upOrDownId") Long upOrDownId,@RequestParam("currentId") Long currentId){
        SysMenu upOrDownMenu = menuService.getById(upOrDownId);
        SysMenu currentMenu = menuService.getById(currentId);
        if (!upOrDownMenu.getMenuType().equals(currentMenu.getMenuType())){
            if ("up".equals(sign)){
                return AjaxResult.error("已经是最顶部了");
            }else {
                return AjaxResult.error("已经是最底部了");
            }
        }
        Integer upOrDownOrderNum = upOrDownMenu.getOrderNum();
        upOrDownMenu.setOrderNum(currentMenu.getOrderNum());
        currentMenu.setOrderNum(upOrDownOrderNum);
        menuService.updateById(upOrDownMenu);
        menuService.updateById(currentMenu);
        return AjaxResult.success();
    }


    @PostMapping("/admin/deleteMenuById")
    @ResponseBody
    public AjaxResult deleteMenuById(@RequestParam("menuId") Long menuId){
        SysMenu byId = menuService.getById(menuId);
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",byId.getId());
        List<SysMenu> list = menuService.list(queryWrapper);
        if (list.size() > 0){
            return AjaxResult.error("该菜单下存在数据, 不允许删除");
        }
        menuService.removeById(byId.getId());
        return AjaxResult.success();
    }

    @GetMapping("/admin/menu-edit")
    public String menuEdit(@RequestParam("menuId") Long menuId, Model model){
        model.addAttribute("menuId",menuId);
        return "admin/system/menu/menu-edit";
    }


    @GetMapping("/admin/getById")
    @ResponseBody
    public AjaxResult getById(@RequestParam("menuId") Long menuId){
        SysMenu byId = menuService.getById(menuId);
        return AjaxResult.success(byId);
    }
}
